package com.xn.app.muying.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xn.app.muying.R;
import com.xn.app.muying.adpter.ChanJianDetailAdapter;
import com.xn.app.muying.bean.ChanJianListBean;
import com.xn.app.muying.bean.ChanJianMessageBean;
import com.xn.app.muying.fragment.ChanJianFragment;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.BaseActivity;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.NetworkUtil;
import com.xn.app.muying.util.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 产检详情
 * Created by Administrator on 2016/10/27.
 */
public class ChanJianDetailActivity extends BaseActivity implements TabLayout.OnTabSelectedListener {

    //标题
    private TextView title=null;
    //返回键
    private ImageView img_back=null;
    //显示当前的页面
    private ViewPager viewPage=null;
    //顶部Tab栏
    private TabLayout tabLayout=null;
    //顶部Tab栏的值
    private ArrayList<String> titles =new ArrayList<>();
    //数据集合
    private ArrayList<String> list=new ArrayList<>();
    //保存用户点击的第几项
    private int listItem=0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_chanjian_detail);
        //初始化控件
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        //初始化控件
        title= (TextView) findViewById(R.id.title_name);
        img_back= (ImageView) findViewById(R.id.img_back);
        viewPage = (ViewPager) findViewById(R.id.viewpage);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        //设置数据
        getData(getIntent().getStringExtra("data"));
        listItem=getIntent().getIntExtra("listItem",0);
        //设置标题
        title.setText("孕期检查");
        //设置返回键点击事件
        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    /**
     *
     * @param data
     */
    private void getData(final String data) {
     new Thread(new Runnable() {
         @Override
         public void run() {
             try {
                 JSONObject jsonObject = new JSONObject(data);
                 JSONArray antenatalCares = jsonObject.getJSONArray("antenatalCares");
                 for (int i = 0; i < antenatalCares.length(); i++) {
                     jsonObject = antenatalCares.getJSONObject(i);
                     String title = jsonObject.optString("title");
                     //将数据添加到集合
                     list.add(jsonObject.toString());
                     //将标题添加到集合
                     titles.add(title);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //设置数据显示
                                MyAdapter adapter = new MyAdapter(getSupportFragmentManager());
                                tabLayout.setOnTabSelectedListener(ChanJianDetailActivity.this);
                                viewPage.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
                                tabLayout.setTabsFromPagerAdapter(adapter);
                                tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
                                viewPage.setOffscreenPageLimit(1);
                                viewPage.setAdapter(adapter);
                                viewPage.setCurrentItem(listItem);
                        }
                    });
                 }

             } catch (JSONException e) {
             }
         }
     }).start();
    }

    /**
     * ViewPage适配器
     */
    private class MyAdapter extends FragmentPagerAdapter {
        MyAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }
        @Override
        public CharSequence getPageTitle(int position) {
            // TODO Auto-generated method stub
            return titles.get(position);
        }
        @Override
        public Fragment getItem(int arg0) {
            // TODO Auto-generated method stub
            return ChanJianFragment
                    .newInstance(list.get(arg0));
        }
        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return titles.size();
        }
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        viewPage.setCurrentItem(tab.getPosition(), true);
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

        @Override
    protected void onResume() {
        super.onResume();
    }
}
